//
//  SQOrderRechangeViewController.m
//  pai
//
//  Created by pro on 14-12-10.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQOrderRechangeViewController.h"

@interface SQOrderRechangeViewController ()

@end

@implementation SQOrderRechangeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:@"订单详情"];
    
    
    _orderView= [SQRechangeOrderView initmyview];
    _orderView.frame = CGRectMake(0,  self.MyNavigationBar.frame.origin.y+self.MyNavigationBar.frame.size.height, 320, 320);
    [_orderView setItem:self.myorder image:self.imageUrl];
    
    [self.view addSubview:_orderView];
    NSLog(@"%@",_orderView.addressButton);
    [_orderView.addressButton addTarget:self action:@selector(onActionSetAddress:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"add btn2");
}
-(void)addressSelected:(addr_t)address
{
    NSLog(@"order addressSelected");
    NSLog(@"%s",self.myorder.serialnum);
    NSLog(@"%d",address.address_id);
    self.address = address;
    
    set_addr_t addr;
    addr.base.msgtype = 8026;
    addr.base.len = sizeof(set_addr_t);
    addr.base.error = 0;
    
    addr.address_id = address.address_id;
    strcpy(addr.serialnum, self.myorder.serialnum);
    
    NSMutableData* msgData = [NSMutableData data];
    [msgData appendBytes:&addr length:sizeof(set_addr_t)];
    
    [super sendData:msgData tag:2 withHUD:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setItemWithObject:(order_t)order image:(NSString*)imageUrl
{
    self.myorder = order;
    self.imageUrl = imageUrl;
}
-(void)onActionSetAddress:(UIButton*)btn
{
    //[self addressSelected:nil];
    SQAddressListViewController *alvc = [[SQAddressListViewController alloc]init];
    alvc.delegate = nil;
    alvc.delegate =self;
    alvc.type = @"select";
    [self.navigationController pushViewController:alvc animated:YES];
}
//读取数据
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    acs_base_t base;
    [data getBytes:&base length:sizeof(acs_base_t)];
    NSString* msg;
    if (base.error == 3080) {
        msg = @"地址设置失败";
    }else if (base.error == 3081) {
        msg = @"地址设置失败";
    }else if (base.error == 3082) {
        msg = @"地址设置失败";
    }else if (base.error == ACS_SUCCESS) {
        msg = @"地址设置成功";
    }
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        self.HUD.labelText = msg;
        sleep(2);
        [self.HUD show:NO];
    } completionBlock:^{
        if (base.error == ACS_SUCCESS) {
            //隐藏
            [_orderView.addressButton removeFromSuperview];
            //显示
            [_orderView setAddress:self.address];
        }
    }];
}
@end
